package com.zhongbei.controller;

import com.zhongbei.pojo.Branch;
import com.zhongbei.service.BranchService;
import com.zhongbei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/branch")
public class BranchController {

    @Autowired
    private BranchService branchService;

    @PostMapping
    public Result<String> create(@RequestBody Branch branch) {
        int i = branchService.insertBranch(branch);
        return i == 1 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @DeleteMapping("/{branchId}")
    public Result<String> delete(@PathVariable Integer branchId) {
        int i = branchService.deleteBranch(branchId);
        return i == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/{branchId}")
    public Result<String> update(@PathVariable Integer branchId, @RequestBody Branch branch) {
        branch.setBranchId(branchId);
        int i = branchService.updateBranch(branch);
        return i == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping
    public Result<List<Branch>> listAll() {
        return Result.success(branchService.selectBranch());
    }

    @GetMapping("/{branchId}")
    public Result<Branch> getById(@PathVariable Integer branchId) {
        return Result.success(branchService.selectBranchByBranchId(branchId));
    }

    @GetMapping("/name/{branchName}")
    public Result<List<Branch>> getByName(@PathVariable String branchName) {
        return Result.success(branchService.selectBranchByBranchName(branchName));
    }

    @GetMapping("/parent/{parentBranchId}")
    public Result<List<Branch>> getByParent(@PathVariable Integer parentBranchId) {
        return Result.success(branchService.selectBranchByParentBranchId(parentBranchId));
    }
}
